package com.tarena.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.tarena.dao.UserDao;
import com.tarena.entity.Booking;
import com.tarena.entity.User;
import com.tarena.service.UserService;

/**
 * liubiao
 */
@Service
public class UserServiceImpl implements UserService {
	
	
	@Resource
	UserDao us;
	
	
	
	public boolean save(User user) {
		Integer sum = us.getSum();
		us.save(user);
		if (sum < us.getSum()) {
			return true;
		}
		return false;
	}

	public boolean delete(Integer id) {
		Integer sum = us.getSum();
		us.delete(id);
		if (sum > us.getSum()) {
			return true;
		}
		return false;
	}

	public void update(User user) {
		us.update(user);
	}

	public List<User> findById(Integer id) {
		List<User> list = us.findById(id);
		return list;
	}

	public List<User> findAllUser() {
		List<User> list = us.findAllUser();
		return list;
	}
	
	public Integer getSum() {
		return us.getSum();
	}

	public List<User> findByPage(int offset, int limit, User user) throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<User> list = session.selectList("findUserByParam", user, rb);
		session.close();
		return list;
	}
	
	public List<User> findByPage(int offset, int limit) throws Exception {
		RowBounds rb = new RowBounds(offset, limit);
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		SqlSessionFactory sf = (SqlSessionFactory) ctx.getBean(SqlSessionFactory.class);
		SqlSession session = sf.openSession();
		List<User> list = session.selectList("findUser", null, rb);
		session.close();
		return list;
	}

	public User login(User user) {
		List<User> list = us.getAccount(user.getAccount());
		if (0 != list.size()) {
			for (User user2 : list) {
				if (user.getPassword().equals(user2.getPassword())) {
					return user2;
				}
			}
			return null;
		} else {
			return null;
		}
		
	}

	public List<Booking> getMeettingroomByUserId(Integer id) {
		User findUserAndBooking = us.findUserAndBooking(id);
		if(findUserAndBooking != null){
			List<Booking> bookings = findUserAndBooking.getBookings();
			return bookings;
		}
		return null;
		
	}

	public List<User> findEmp(User user) {
		return us.findEmp(user);
	}

	public List<User> getAccount(String account) {
		return us.getAccount(account);
	}

	public User findUserAndEmp(int id) {
		return us.findUserAndEmp(id);
	}

	public User findUserAndBooking(Integer id) {
		return us.findUserAndBooking(id);
	}

}
